package lvbu.wang.lvbuoperator.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import lvbu.wang.lvbuoperator.R;


/**
 * Created by DaiQing.Zha on 2016/6/30 0030.
 * Email:1642674371@qq.com
 * Description:
 */
public class CustDialog extends Dialog {

    public CustDialog(Context context) {
        super(context);
    }

    public CustDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public CustDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder{

        private Context context;	//上下文环境
        private View view;
        private OnClickListener cancelDialogButtonClickListener,okDialogButtonClickListener;
        public Builder(Context context) {
            this.context = context;
        }

        public Builder(Context context,View view) {
            this.context = context;
            this.view = view;
        }
        public Builder setCancelDialogButton(OnClickListener listener) {

            this.cancelDialogButtonClickListener = listener;
            return this;
        }
        public Builder setOKDialogButton(OnClickListener listener) {

            this.okDialogButtonClickListener = listener;
            return this;
        }
        public CustDialog create() {

            final View layout;
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustDialog dialog = new CustDialog(context, R.style.Dialog);
            if (view == null){

                layout = inflater.inflate(R.layout.view_cuscomm_dialog, null);
            }else{

                layout = view;
            }
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            dialog.setContentView(layout);
            dialog.setCanceledOnTouchOutside(false);	//设置dialog点击周围时不消失
            if (cancelDialogButtonClickListener == null){

                ((Button) layout.findViewById(R.id.btn_cancel))
                        .setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });
            }else{

                ((Button) layout.findViewById(R.id.btn_cancel))
                        .setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                cancelDialogButtonClickListener.onClick(dialog,
                                        DialogInterface.BUTTON_POSITIVE);
                            }
                        });
            }
            if (okDialogButtonClickListener != null) {
                ((Button) layout.findViewById(R.id.btn_ok))
                        .setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                okDialogButtonClickListener.onClick(dialog,
                                        DialogInterface.BUTTON_NEGATIVE);
                            }
                        });
            }
            return dialog;
        }
    }
}
